<?php


namespace App\Modules\Employee\Transformers;


use League\Fractal\TransformerAbstract;

class GetMostPopularStoreTransformer extends TransformerAbstract
{
    private $list;

    public function __construct($list)
    {
        $this->list = $list;
    }

    public function transform()
    {
        foreach ($this->list->items() as $v) {
            if (isset($v->StoreResource)) {
                foreach ($v->StoreResource as $j) {
                    $j->url;
                }
            }
        }
	    $newList = [];
        $this->list = $this->list->toArray();

        if (!empty($this->list['data'])) {
            foreach ($this->list['data'] as $v) {
//	            if ($v['store_resource_count'] == 3) {
		            if (isset($v['picture']['image'])) {
			            $v['logo'] = $v['picture']['image'];
			            unset($v['picture']);
		            }
		            $resourceList = $v['store_resource'];
		            $v['store_resource'] = array_map(function ($j) {
			            return [
				            'is_video' => isset($j['url']['url']) ? 1 : 0,
				            'video'    => isset($j['url']['url']) ? $j['url']['url'] : '',
				            'image'    => isset($j['url']['thumb']) ? $j['url']['thumb'] : (isset($j['url']['image']) ? $j['url']['image'] : '')
			            ];
		            }, $resourceList);
		            $isVideo = array_sort(array_column($v['store_resource'], 'is_video'), 'desc');
		            array_multisort($isVideo, SORT_DESC, $v['store_resource']);
		            $newList[] = $v;
//	            }
            }
        }
        $this->list['data'] = $newList;
        return $this->list;
    }

}
